package lyx.game.element
{
	import flash.geom.Rectangle;
	
	import lyx.game.utils.Direction;
	import lyx.utils.log.Logger;

	/**
	 * DynamicGameElement是具有运动性的元素。
	 * 控制元素运动的三个核心是：steps，direction，step_speed
	 */
	public class DynamicGameElement extends GameElement
	{
		/**
		 * 元素可运动的步数，当steps不为0时，元素move函数才能生效。
		 */
		public var steps:int = 0;

		/**
		 * 元素运动速度，可以理解成 (step_speed像素每秒) 
		 */		
		public var step_speed:int = 50;
		
		/**
		 * 元素运动的范围，暂时只需要设置Rectangle的width和height 
		 */		
		public var active_area:Rectangle;
		
		protected var _direction:int = -1;
		
		protected var _moveFunction:Function = null;
		
		protected var _last_x:Number;
		protected var _last_y:Number;
		
		public function DynamicGameElement(x:Number,y:Number,direction:int)
		{
			this.x = x;
			this._last_x = x;
			this.y = y;
			this._last_y = y;
			this.direction = direction;
		}
		
		/**
		 * 返回DynamicGameElement的运动方向 
		 * @return 
		 * 
		 */		
		public function get direction():int
		{
			return _direction;
		}
		/**
		 *	对direction的赋值，会使DynamicGameElement的moveFunction变更 
		 * @param new_direction
		 * 
		 */		
		public function set direction(new_direction:int):void
		{
			if(_direction!=new_direction||_moveFunction==null)
			{
				_direction = new_direction;
				switch(_direction)
				{
					case Direction.EAST:
						_moveFunction = function(time:int):void
										{
											x+=step_speed*time/1000.0;
										};
						break;
					case Direction.EAST_SOUTH:
						_moveFunction = function(time:int):void
										{
											x+=step_speed*time/1000.0;
											y+=step_speed*time/1000.0;
										};
						break;
					case Direction.SOUTH:
						_moveFunction = function(time:int):void
										{
											y+=step_speed*time/1000.0;
										};
						break;
					case Direction.WEST_SOUTH:
						_moveFunction = function(time:int):void
										{
											x-=step_speed*time/1000.0;
											y+=step_speed*time/1000.0;
										};
						break;
					case Direction.WEST:
						_moveFunction = function(time:int):void
										{
											x-=step_speed*time/1000.0;
										};
						break;
					case Direction.WEST_NORTH:
						_moveFunction = function(time:int):void
										{
											x-=step_speed*time/1000.0;
											y-=step_speed*time/1000.0;
										};
						break;
					case Direction.NORTH:
						_moveFunction = function(time:int):void
										{
											y-=step_speed*time/1000.0;
										};
						break;
					case Direction.EAST_NORTH:
						_moveFunction = function(time:int):void
										{
											x+=step_speed*time/1000.0;
											y-=step_speed*time/1000.0;
										};
						break;
					default:
						_moveFunction = null;
						break;
				}
				_view_updated = true;
			}
		}
		
		/**
		 * 元素运动接口。函数每调用一次，
		 * 只要steps不为0的情况下，无论元素有无移动，steps都会减一。
		 */
		public function move(time:int):void
		{
			if(steps>0)
			{
				if(_moveFunction!=null)
				{
					_last_x = x;
					_last_y = y;
					_moveFunction.call(this,time);
				}
				if(isOutActiveArea())
				{
					outActiveAreaHandler();
				}
				steps--;
			}
		}
		
		private function isOutActiveArea():Boolean
		{
			if(active_area==null || view_data==null)
			{
				return false;
			}
			else
			{
				if( x<0
					|| (x+view_data.width)>active_area.width 
				    || y<0
					|| (y+view_data.height)>active_area.height)
				{
					return true;
				}
				return false;
			}
		}
		
		/** 
		 * DynamicGameElement在调用move()方法时，可能会激发outActiveAreaHandler()，
		 * 你可以通过重写此方法来实现对DynamicGameElement超出active_area的处理。
		 */		
		protected function outActiveAreaHandler():void
		{
			Logger.getInstance().debug("No implement outActiveAreaHandler() function");
		}
		
		/**
		 * 使DynamicGameElement恢复到调用move前的位置。
		 * 对于每一次move()的调用，moveRollBack()也是一次调用有效。
		 */
		public function moveRollBack():void
		{
			x = _last_x;
			y = _last_y;
		}
	}
}